﻿using UnityEngine;
using System;
using System.Linq;
using System.Collections;

namespace Hont.UMotionGraph
{
    public class MGSoundEffector : MoGraphEffector
    {
        public AudioSource audioSource;
        public float force = 1;
        [HideInInspector]
        public float[] soundSampleArr;
        Vector3[] scalePositionArr;


        public override void Init(IMoGraphContainer container)
        {
            base.Init(container);

            scalePositionArr = new Vector3[container.Count];

            for (int i = 0; i < scalePositionArr.Length; i++)
            {
                scalePositionArr[i] = UnityEngine.Random.insideUnitSphere;
            }

            soundSampleArr = new float[128];
        }

        public override Vector3 Execute(int inx, Vector3 inValue, float fallOffProportion)
        {
            audioSource.GetSpectrumData(soundSampleArr, 0, FFTWindow.BlackmanHarris);
            return inValue + scalePositionArr[inx] * (force * soundSampleArr.Max() * fallOffProportion);
        }
    }
}
